package com.tool_stdy.IO.StreamTest;

import java.io.*;

/**
 * Created by yan_li on 2017/1/10.
 */
public class TestInputOutStream {

    public static void main(String[] args) throws Exception {
        String srcPath="f:/test/";
        String targetPath = "f:/target/";
        copyFileDri(new File(srcPath),new File(targetPath));
    }

    public static void copyFile(File srcfile,File outfile) throws Exception {
        FileInputStream input = new FileInputStream(srcfile);
        BufferedInputStream in = new BufferedInputStream(input);
        FileOutputStream output = new FileOutputStream(outfile);
        BufferedOutputStream out = new BufferedOutputStream(output);
        byte[] readBuffer = new byte[1024];
        int len=0;
        while(-1!=(len = in.read(readBuffer))){
            out.write(readBuffer,0,len);
        }
        out.flush();
        out.close();
    }

    public static void copyFileDri(File srcfile,File outfile) throws Exception {
        if(srcfile.isFile()){
            copyFile(srcfile,outfile);
        }else if(srcfile.isDirectory()){
            if(outfile.exists()){
                System.out.println(outfile.getPath()+"存在");
                return ;
            }
            outfile.mkdirs();
            for(File f :srcfile.listFiles()){
                copyFileDri(f,new File(outfile,f.getName()));
            }
        }
    }
}
